<?php
require SYSTEM_PATH . '/Input.php'; // get post请求参数类
/**
 * 控制器抽象类
 *
 * @author vincent
 *        
 */
abstract class Controller {
	/**
	 * get、post参数
	 *
	 * @var Input
	 */
	protected $input;
	/**
	 * 构造函数
	 */
	public function __construct() {
		// 全部controller共享一个input实例
		$this->input = Input::getInstance ();
	}
	/**
	 * 析构函数
	 */
	public function __destruct() {
	}
	/**
	 * 返回视图 视图使用smarty模板
	 *
	 * @param string $viewPath
	 * @param array $variables
	 */
	public function render($viewName, $variables = array()) {
		$config = &$GLOBALS ['CONFIG'];
		$viewPath = MODULE_PATH . '/views/' . $viewName . '.' . $config ['SMARTY_TEMPLATE_SUFFIX'];
		import ( VENDOR_PATH . '/smarty/libs/Smarty.class.php' );
		$smarty = new Smarty ();
		$smarty->left_delimiter = $config ['SMARTY_LEFT_DELIMITER'];
		$smarty->right_delimiter = $config ['SMARTY_RIGHT_DELIMITER'];
		// 绑定参数
		foreach ( $variables as $key => &$value ) {
			$smarty->assign ( $key, $value );
		}
		$smarty->compile_dir = APPLICATION_PATH . '/runtime/templet';
		$smarty->display ( $viewPath );
		exit ();
	}
	
	/**
	 * 返回文本视图
	 *
	 * @param string $text
	 */
	public function renderText($text, $charset = 'utf-8') {
		header ( 'Content-type: text/plain; charset=' . $charset );
		echo $text;
		exit ();
	}
	
	/**
	 * 返回json视图
	 *
	 * @param array $data
	 */
	public function renderJSON($data, $charset = 'utf-8') {
		header ( 'Content-type: text/json; charset=' . $charset );
		echo json_encode ( $data, JSON_UNESCAPED_UNICODE );
		exit ();
	}
	/**
	 * 重定向
	 *
	 * @param string $url
	 * @param string|array $params
	 * @param integer $time
	 * @param string $msg
	 */
	public function redirect($url = '', $params = '', $time = 0, $msg = '') {
		header ( 'Content-type: text/html; charset=utf-8' );
		$absoluteUrl = base_url ();
		if (! empty ( $url )) {
			$absoluteUrl .= '/' . $url;
		}
		if (! empty ( $params )) {
			if (is_array ( $params )) {
				$absoluteUrl .= '?' . http_build_query ( $params );
			}
			else if (is_string ( $params )) {
				$absoluteUrl .= '?' . $params;
			}
		}
		if (empty ( $msg )) {
			$msg = "系统将在{$time}秒之后自动跳转到{$url}！";
		}
		
		if ($time == 0) {
			header ( 'location: ' . $absoluteUrl );
		}
		else {
			header ( "refresh:{$time};url={$absoluteUrl}" );
			echo ($msg);
		}
		exit ();
	}
	/**
	 * 返回html视图
	 *
	 * @param string $html
	 * @param string $charset
	 */
	public function renderHTML($html, $charset = 'utf-8') {
		header ( 'Content-type: text/html; charset=' . $charset );
		echo $html;
		exit ();
	}
	/**
	 * 加载模型
	 *
	 * @param string $modelName
	 * @return Model
	 */
	public function loadModel($modelName) {
		return Mikasa::getModelInstance ( $modelName );
	}
	/**
	 * 转发请求
	 *
	 * @param string $controllerName 控制器名
	 * @param string $actionName 动作名
	 */
	public function forward($controllerName, $actionName) {
		$controllerName .= 'Controller';
		$actionName .= 'Action';
		$controller = Mikasa::getControllerInstance ( $controllerName );
		$controller->$actionName ();
		exit ();
	}
	
	/**
	 * 调用不存在的方法时调用
	 *
	 * @param string $actionName
	 * @param array $params
	 * @throws Exception
	 */
	public function __call($actionName, $params) {
		throw new Exception ( CONTROLLER_NAME . '不存在方法' . $actionName, 404 );
	}
}
